package com.vclee.fast.utils.sdk;


import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.net.CallRet;
import com.qiniu.api.rs.GetPolicy;
import com.qiniu.api.rs.PutPolicy;
import com.qiniu.api.rs.RSClient;
import com.qiniu.api.rs.URLUtils;
import com.vclee.fast.utils.exception.RRException;
import org.apache.commons.codec.EncoderException;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class QiNiuUtil {

    private  static String ACCESS_KEY;
    private  static String SECRET_KEY;
    private  static String BUCKET_NAME;
    private  static String DOMAIN;

    @Value("${custom.QIN_OSS_KEY}")
    public  void setAccessKey(String accessKey) {
        ACCESS_KEY = accessKey;
    }

    @Value("${custom.QIN_OSS_SECRET}")
    public  void setSecretKey(String secretKey) {
        SECRET_KEY = secretKey;
    }

    @Value("${custom.QIN_OSS_BUCKET}")
    public  void setBucketName(String bucketName) {
        BUCKET_NAME = bucketName;
    }

    @Value("${custom.QIN_OSS_DOMAIN}")
    public  void setDOMAIN(String DOMAIN) {
        QiNiuUtil.DOMAIN = DOMAIN;
    }

    /**
     * 上传token生成
     *  <a href="https://developer.qiniu.com/kodo/manual/1272/form-upload">前端参考</a>
     *  <a href="https://developer.qiniu.com/kodo/manual/1671/region-endpoint">前端上传地址</a>
     * 前端 const formData = new FormData();
     * formData.append('file', file);
     * formData.append('key', fileName); // BUCKET_NAME/filePath eg:ios/202007/4abc1df9rty.jpg
     * formData.append('token',token)
     *
     * @return String
     */
    public static String getToken() {
        try {
            Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
            PutPolicy putPolicy = new PutPolicy(BUCKET_NAME);
            putPolicy.expires = 120; // 设置token过期时间120秒
//            putPolicy.callbackBody = ;
            return putPolicy.token(mac);
        } catch (AuthException | JSONException e) {
            throw new RRException("七牛云token获取异常", 402);
        }
    }

    /**
     * 获取图片预览地址
     * 参考文档
     * <a href="https://developer.qiniu.com/dora/api/1279/basic-processing-images-imageview2">...</a>
     *
     * @param object 相对地址 eg:202007/4abc1df9rty.jpg
     * @param width  默认宽度
     * @param height (height>0表示需要裁剪显示)
     * @return String
     * eg: <a href="http://images.hanjiapz.com/upload/20220910/ee6c2d02ea79422d8bf32a9398271ae1.jpg?imageMogr2/thumbnail/200x">...</a>
     */
    public static String getImgPreviewUrl(String object, Integer width, Integer height) {
        try {
            Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
            String baseUrl = URLUtils.makeBaseUrl(DOMAIN, object);
            GetPolicy getPolicy = new GetPolicy();
            getPolicy.expires = 3600 * 24;
            String downloadUrl = getPolicy.makeRequest(baseUrl, mac);
            if (height>0) {
                return downloadUrl + "?imageView2/1/w/" + width + "/h/" + height;
            } else {
                return downloadUrl + "?imageView2/2/w/" + width;
            }
        } catch (EncoderException | AuthException e) {
            throw new RRException("七牛云图片获取异常", 402);
        }
    }

    /**
     * 获取原文件访问url
     *
     * @param object 文件相对路径
     * @return 源文件url
     */
    public static String getOriFileUrl(String object) {
        try {
            Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
            String baseUrl = URLUtils.makeBaseUrl(DOMAIN, object);
            GetPolicy getPolicy = new GetPolicy();
            getPolicy.expires = 3600 * 24;
            return getPolicy.makeRequest(baseUrl, mac)+"?imageslim";
        } catch (EncoderException | AuthException e) {
            throw new RRException("七牛云图片获取异常", 402);
        }
    }

    /**
     * 文件删除
     *
     * @param filePath 文件相对路径 eg:202007/4abc1df9rty.jpg
     * @return 是否将已存在的文件进行了删除
     */
    public static boolean delFile(String filePath) {
        Mac mac = new Mac(ACCESS_KEY, SECRET_KEY);
        RSClient client = new RSClient(mac);
        CallRet ret = client.delete(BUCKET_NAME, filePath);
        return ret.ok();
    }
}

